<?php
/**
 * 数据大小单位转换
 * @param $byte
 * @return string
 */
function size_count($byte)
{
    if ($byte < 1024) {
        return $byte . 'byte';
    } elseif (($size = round($byte / 1024, 2)) < 1024) {
        return $size . 'KB';
    } elseif (($size = round($byte / (1024 * 1024), 2)) < 1024) {
        return $size . 'MB';
    } else {
        return round($byte / (1024 * 1024 * 1024), 2) . 'GB';
    }
}

/**
 * php弹出框
 * @param $data             弹出的信息
 * @param string $type      弹出框的类型
 * @param bool $is_reload   是否刷新
 * @param string $url       新页面打开地址
 */
function php_alert($data, $type = 'error', $is_reload = false, $url = '')
{
    if (empty($url) && !$is_reload) {
        echo "<script>parent.my_alert('$data','$type')</script>";
        die;
    } else if (empty($url) && $is_reload) {
        echo "<script>parent.my_alert('$data','$type',function() {
            parent.location.reload();
})</script>";
    } else {
        echo "<script>parent.my_alert('$data','$type',function(){parent.location.href='$url';});</script>";
        die;
    }
}

/**
 * 添加图片
 * @param $file             图片文件
 * @return bool|string      返回值（false为错误）
 */
function add_img($file)
{
    if (!empty($file)) {
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if ($info) {
            $img_name = $info->getSaveName();
            $img_name = "/public/uploads/" . $img_name;
            return $img_name;
        }else{
            php_alert("图片信息丢失,请刷新页面后再次上传");
        }
    } else {
        php_alert("图片信息错误,请刷新页面后再次上传");
    }
}


/**
 * 上传并替换图片
 * @param $file         图片文件
 * @param $obj_db       数据表名
 * @param $obj_field    字段
 * @return string       文件名
 */
function replace_img($file, $obj_db, $obj_field)
{
    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
    $file_url = db($obj_db)->field($obj_field)->find();
    del_img($file_url[$obj_field]);
    $img_info = $info->getSaveName();
    $img_info = "/public/uploads/" . $img_info;
    return $img_info;
}

/**
 * 删除图片
 * @param $target
 * @return bool
 */
function del_img($target)
{
    $target1 = substr(str_replace("\\", "/", $target), 1);
    if (file_exists($target1)) {
        unlink("." . $target);
    }
    return true;
}

/**
 * 设置网页标题
 * @param string $action_name
 * @return string
 */
function get_title($action_name = "index")
{
    switch (true) {
        case $action_name == "index";
            $now_title = "首页";
            break;
        case $action_name == "all_product";
            $now_title = "全部产品";
            break;
        case $action_name == "about";
            $now_title = "关于我们";
            break;
        case $action_name == "news";
            $now_title = "KR新闻";
            break;
        case $action_name == "story";
            $now_title = "品牌故事";
            break;
        case $action_name == "health_info";
            $now_title = "健康资讯";
            break;
        case $action_name == "health_article";
            $now_title = "健康文章";
            break;
        case $action_name == "product_detail";
            $now_title = "产品详情";
            break;
        case $action_name == "product_collocation";
            $now_title = "产品搭配";
            break;
        default:
            $now_title = "";
            break;
    }
    return $now_title;
}

//curl的get请求
function httpGet($url,$https=true){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $https?2:0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $https?2:0);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    if ($res === FALSE){//错误捕捉
        $res = curl_error($curl);
    }
    curl_close($curl);
    return $res;
}
//返回数据函数
function returnData($res,$msg,$data=[]){
    $returnData = [
        "res"=>$res,
        "msg"=>$msg,
        "data"=>$data
    ];
    return json($returnData);
}